package main

import (
	"os"

	"irisx/handle"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/middleware/accesslog"
	"github.com/kataras/iris/v12/middleware/recover"
)

func main() {
	app := iris.New()
	app.Use(iris.Compression)
	app.UseRouter(recover.New())
	log(app)
	routers(app)
	app.Listen(":8080")
}

func log(app *iris.Application) {
	//f, _ := os.Create("iris.log")
	//app.Logger().SetOutput(f)
	ac := makeAccessLog()
	defer ac.Close() // Close the underline file.
	app.UseRouter(ac.Handler)
}
func routers(app *iris.Application) {
	party := app.Party("/")
	handle.Resp{}.InitRoute(party)
	handle.Protobuf{}.InitRoute(party)
	handle.Req{}.InitRoute(party)
	handle.Redirect{}.InitRoute(party)
	handle.File{}.InitRoute(party)
}

// Read the example and its comments carefully.
func makeAccessLog() *accesslog.AccessLog {
	// Initialize a new access log middleware.
	ac := accesslog.File("./access.log")
	// Remove this line to disable logging to console:
	ac.AddOutput(os.Stdout)

	// The default configuration:
	ac.Delim = '|'
	ac.TimeFormat = "2006-01-02 15:04:05"
	ac.Async = false
	ac.IP = true
	ac.BytesReceivedBody = true
	ac.BytesSentBody = true
	ac.BytesReceived = false
	ac.BytesSent = false
	ac.BodyMinify = true
	ac.RequestBody = true
	ac.ResponseBody = false
	ac.KeepMultiLineError = true
	ac.PanicLog = accesslog.LogHandler

	// Default line format if formatter is missing:
	// Time|Latency|Code|Method|Path|IP|Path Form Query Fields|Bytes Received|Bytes Sent|Request|Response|
	//
	// Set Custom Formatter:
	ac.SetFormatter(&accesslog.JSON{
		Indent:    "  ",
		HumanTime: true,
	})
	// ac.SetFormatter(&accesslog.CSV{})
	// ac.SetFormatter(&accesslog.Template{Text: "{{.Code}}"})

	return ac
}
